package com.g8.pethospitallearnbackend.service.impls;

import com.g8.pethospitallearnbackend.entity.Role;
import com.g8.pethospitallearnbackend.entity.RoleExample;
import com.g8.pethospitallearnbackend.exception.CustomException;
import com.g8.pethospitallearnbackend.exception.NoDataException;
import com.g8.pethospitallearnbackend.mapper.RoleMapper;
import com.g8.pethospitallearnbackend.service.FlowService;
import com.g8.pethospitallearnbackend.service.RoleService;
import com.g8.pethospitallearnbackend.utils.NewIdGenerateUtil;
import com.g8.pethospitallearnbackend.utils.responseUtil.ResponseStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private FlowService flowService;

//    @Override
//    public List<Role> getAllRoles(int pageNow, int pageSize) {
//        PageHelper.startPage(pageNow, pageSize);
//        RoleExample roleExample = new RoleExample();
//        roleExample.createCriteria().andRoleNameIsNotNull();
//        List<Role> roleList = roleMapper.selectByExample(roleExample);
//        PageInfo<Role> rolePageInfo = new PageInfo<>(roleList);
//        return rolePageInfo.getList();
//    }

    @Override
    public List<Role> getAllRoles() {
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andRoleIdIsNotNull();
        List<Role> roleList = roleMapper.selectByExampleWithBLOBs(roleExample);
        if (roleList.isEmpty()) throw new NoDataException();
        return roleList;
    }

    @Override
    public Role getARole(String roleIdOrName) {
        Role role = roleMapper.selectByPrimaryKey(roleIdOrName);
        if(role == null) {
            RoleExample roleExample = new RoleExample();
            roleExample.createCriteria().andRoleNameEqualTo(roleIdOrName);
            List<Role> roleList = roleMapper.selectByExampleWithBLOBs(roleExample);
            if (roleList.isEmpty()) throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST, "未查询到相关角色！");
            else if(roleList.size() > 1) throw new CustomException(ResponseStatusEnum.SYSTEM_ERROR, "有多个重复名字的角色");
            role = roleList.get(0);
        }
        role.setRoleFlows(flowService.getFlowsOfARole(role.getRoleId()));
        return role;
    }

    @Override
    public Role addARole(Role role) {
        if (role.getRoleName().isBlank())
            throw new CustomException(ResponseStatusEnum.MISS_PARAM, "缺少角色名字参数！");
        // 校验角色不能重复
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andRoleNameEqualTo(role.getRoleName());
        if (!roleMapper.selectByExampleWithBLOBs(roleExample).isEmpty())
            throw new CustomException(ResponseStatusEnum.BAD_REQUEST, "角色名字重复！");
        // 生成一个id
        String id = NewIdGenerateUtil.getNumber(3);
        Role repeat = roleMapper.selectByPrimaryKey(id);
        while(repeat != null) {
            id = NewIdGenerateUtil.getNumber(3);
            repeat = this.getARole(id);
        }
        role.setRoleId(id);
        roleMapper.insert(role);
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public Role modifyARole(Role role) {
        if (role.getRoleName().isBlank())
            throw new CustomException(ResponseStatusEnum.MISS_PARAM, "缺少角色名字参数！");
        // 校验角色不能重复
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andRoleNameEqualTo(role.getRoleName());
        List<Role> repeatName = roleMapper.selectByExampleWithBLOBs(roleExample);
        if (!repeatName.isEmpty())
            for (Role r : repeatName) {
                if (!r.getRoleId().equals(role.getRoleId()))
                    throw new CustomException(ResponseStatusEnum.BAD_REQUEST, "角色名字重复！");
            }
        if (roleMapper.updateByPrimaryKeyWithBLOBs(role) < 1) throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST, "未查询到待修改角色");
        return getARole(role.getRoleId());
    }

    @Override
    public String deleteARole(String roleId) {
        if (roleMapper.deleteByPrimaryKey(roleId) < 1)
            throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST, "待删除角色不存在！");
        return "删除成功";
    }




}
